﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using ReAnimation.Domain;
using ReAnimation.Helpers.Common;
using ReAnimation.MVP.ModelInterfaces;
using ReAnimation.MVP.Presenters;
using ReAnimation.MVP.ViewInterfaces;

namespace ReAnimation.MVP.Views
{
    public partial class LinkingDialog : BaseFormView, ILinkingDialogView
    {
        private IMainPresentationModel _mainPresentationModel;
        private IMainModel _mainModel;
        private LinkingDialogPresenter _linkingDialogPresenter;

        public LinkingDialog(IMainPresentationModel mainPresentationModel,
                             IMainModel mainModel,
                             LinkingDialogPresenter linkingDialogPresenter)
        {
            InitializeComponent();
            _mainPresentationModel = mainPresentationModel;
            _mainModel = mainModel;
            InitControls();
            linkingDialogPresenter.View = this;
        }

        private void InitControls()
        {
            lblAtom1.Text = GetDisplayNameForAtom(_mainPresentationModel.CurrentlySelectedForLinkingAtoms.Value1);
            lblAtom2.Text = GetDisplayNameForAtom(_mainPresentationModel.CurrentlySelectedForLinkingAtoms.Value2);

            cmbLinkTypes.DataSource = PrepareLinkTypesDataSource();
        }

        private static String GetDisplayNameForAtom(Atom atom)
        {
            return "{0}(number?)".F(atom.AtomDescription.Name);
        }

        private IList<object> PrepareLinkTypesDataSource()
        {
            return (from linkType in Enum.GetValues(typeof (MoleculeLinkType)).Cast<MoleculeLinkType>()
                    select new
                               {
                                   DisplayName = linkType.ToString(),
                                   Value = linkType
                               }).Cast<object>().ToList();
        }

        public event EventHandler Cancel;
        public event EventHandler LinkTypeSelected;
        public event EventHandler ApprovedLinking;
        public MoleculeLinkType SelectedLinkType
        {
            get { return (MoleculeLinkType)cmbLinkTypes.SelectedValue; }
            set { cmbLinkTypes.SelectedValue = value; }
        }

        public void ShowLinkingDialog()
        {
            ShowDialog();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            LinkTypeSelected.Throw(this, EventArgs.Empty);
        }
    }
}
